/**
 * 路由封装
 *
 * 用法：
 * router.push({
 *   name: 'user',  // => /pages/user/index
 *   data: { id: '01' }
 * });
 */
const routes = require('./routers.js')

function push({ name, data }) {
  const dataStr = encodeURIComponent(JSON.stringify(data));
  const route = routes[name];
  // console.log('router push',route);
  if (!route) {
    uni.showToast({
      title: '页面跳转失败',
      icon: 'none',
      mask: true
    })
    return
  }

  const url = route.path
  if (route.type === 'tab') {
    uni.switchTab({
      url: url, // 微信底部tab页面不支持传参
    });
    return;
  }

  /* if (route.type === 'redirectTo') {
    uni.redirectTo({
      url: `${url}?encodedData=${dataStr}`, // 微信底部tab页面不支持传参
    });
    return;
  } */

  uni.navigateTo({
    url: `${url}?encodedData=${dataStr}`,
  });
}

function extract(options) {
  return JSON.parse(decodeURIComponent(options.encodedData));
}

module.exports = {
  routes,
  push,
  extract
};
